package cc.linker.steplink.crm.merchanth5.controller;

import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.service.BillService;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 反润相关
 */
@RequestMapping("/h5/mbl/bill")
@RestController
public class BillController {

    @Autowired
    private BillService billService;

    @Autowired
    private RequestFilter requestFilter;

    /**
     * 反润明细
     * @param pageSize
     * @param pageIndex
     * @param request
     * @return
     * @throws GlobalException
     */
    @GetMapping("/detail")
    public PageInfo<Map<String, Object>> saveGoods(@RequestParam(defaultValue = "20") Integer pageSize,
                                               @RequestParam(defaultValue = "1") Integer pageIndex,
                                               HttpServletRequest request) throws GlobalException {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();

        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(billService.getBalanceDetail(customerId));
    }

    /**
     * 详情
     *
     * @return
     */
    @GetMapping("/info")
    public Map info(String tradeId, HttpServletRequest request) throws GlobalException {
        if (StringUtils.isEmpty(tradeId)) {
            throw GlobalException.build("参数错误");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        return billService.getTradeInfoByTradeId(tradeId, orgId);
    }
}
